package com.topvision.jack.camerasdk2.camera.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.topvision.jack.camerasdk2.R;
import com.topvision.jack.camerasdk2.camera.PhotoLoaderTask;
import com.topvision.jack.camerasdk2.camera.callback.OnShowPhotoListener;
import com.topvision.jack.camerasdk2.camera.callback.PhotoSimpleOnPageChangeListener;
import com.topvision.jack.camerasdk2.camera.commom.CameraConstants;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;

/**
 * 照片预览自定义View
 * 创建时间:2017/12/26
 * 作者 jack
 * 描述
 */
public class TvPhotoView extends ViewPager  {

    int width;
    int height;
    List<String> photos;
    List<View> views;
    ShowPhotoPagerAdapter showPhotoPagerAdapter;
    OnShowPhotoListener onShowPhotoListener;

    public TvPhotoView(Context context) {
        super(context);
    }

    public TvPhotoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = getDefaultSize(0, widthMeasureSpec);
        height = getDefaultSize(0, heightMeasureSpec);
        //Log.e(CameraConstants.TAG, "width=" + width + ",height=" + height);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //initView();

    }

    private void initView() {

        views = new ArrayList<>();
       LayoutInflater layoutInflater =  LayoutInflater.from(getContext());
        for (int i = 0; photos != null && i<photos.size(); i++) {
            View view = layoutInflater.inflate(R.layout.common_photo_view, null);
            PhotoView photo_view = view
                    .findViewById(R.id.photo_view);
            photo_view.setZoomable(true);
            if (i == 0) {
                String filePath = photos.get(i);
                //默认第一张图
                new PhotoLoaderTask(getContext(), photo_view).execute(filePath);
            }
            views.add(view);
        }
        showPhotoPagerAdapter = new ShowPhotoPagerAdapter(views);
        setAdapter(showPhotoPagerAdapter);
        setCurrentItem(0);
        addOnPageChangeListener(new PhotoSimpleOnPageChangeListener(getContext(),this));

    }



    public void showPhoto(List<String> photos ) {
        setPhotos(photos);
        initView();
    }

    public List<String> getPhotos() {
        return photos;
    }

    public void setPhotos(List<String> photos) {
        this.photos = photos;
    }

    public List<View> getViews() {
        return views;
    }

    public void setViews(List<View> views) {
        this.views = views;
    }

    public OnShowPhotoListener getOnShowPhotoListener() {
        return onShowPhotoListener;
    }

    public void setOnShowPhotoListener(OnShowPhotoListener onShowPhotoListener) {
        this.onShowPhotoListener = onShowPhotoListener;
    }
}
